package org.szy.plugin.sys.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.ProxyComponent;
import org.szy.core.common.page.QueryPage;
import org.szy.core.common.param.CommonStatusParam;
import org.szy.core.common.service.impl.BaseServiceImpl;
import org.szy.plugin.sys.entity.SysPosition;
import org.szy.plugin.sys.mapper.SysPositionMapper;
import org.szy.plugin.sys.param.SysPositionQueryParam;
import org.szy.plugin.sys.service.SysOrgService;
import org.szy.plugin.sys.service.SysPositionService;

import java.util.ArrayList;
import java.util.List;

import static org.szy.plugin.sys.entity.table.SysPositionTableDef.SYS_POSITION;

/**
 * 系统职位Service接口实现类
 *
 * @author wangbuhao
 * @since 2023-06-19 20:13:46
 **/
@ProxyComponent
public class SysPositionServiceImpl extends BaseServiceImpl<SysPositionMapper, SysPosition> implements SysPositionService {

    @Inject
    private SysPositionMapper sysPositionMapper;
    @Inject
    private SysOrgService sysOrgService;

    @Override
    public QueryWrapper qwByQuery(SysPositionQueryParam param) {

        QueryWrapper qw = new QueryWrapper();

        if (StrUtil.isNotEmpty(param.getColumnField())) {
            qw.select(param.getColumnField().split(","));
        }
        if (StrUtil.isNotEmpty(param.getOrgId())) qw.and(SYS_POSITION.ORG_ID.eq(param.getOrgId()));
        if (StrUtil.isNotEmpty(param.getOrgIdIn())) {
            List<String> orgIds = sysOrgService.listIdByPid(param.getOrgIdIn());
            if (orgIds.isEmpty()) orgIds = new ArrayList<>(1);
            orgIds.add(param.getOrgIdIn());
            qw.and(SYS_POSITION.ORG_ID.in(orgIds));
        }
        if (StrUtil.isNotEmpty(param.getName())) qw.and(SYS_POSITION.NAME.eq(param.getName()));
        if (StrUtil.isNotEmpty(param.getType())) qw.and(SYS_POSITION.TYPE.eq(param.getType()));
        if (param.getStatus() != null) qw.and(SYS_POSITION.STATUS.eq(param.getStatus()));

        return qw;
    }

    @Override
    public Page<SysPosition> page(QueryPage<SysPosition> page, SysPositionQueryParam param) {
        QueryWrapper qw = qwByQuery(param);

        // 正序排序
        if (StrUtil.isNotEmpty(page.getAscField())) qw.orderBy(page.getAscField().split(","));
        // 倒叙排序
        if (StrUtil.isNotEmpty(page.getDescField())) {
            for (String desc : page.getDescField().split(",")) {
                qw.orderBy(new QueryColumn(desc).desc());
            }
        }

        return this.page(page, qw);
    }

    @Override
    public boolean add(SysPosition entity) {
        if (entity.getSort() == null) {
            long count = count();
            entity.setSort(Convert.toInt(count, 0) + 1);
        }
        return this.save(entity);
    }

    @Override
    public boolean modifyStatus(CommonStatusParam param) {
        SysPosition position = UpdateEntity.of(SysPosition.class, param.getId());
        position.setStatus(param.getStatus());
        return sysPositionMapper.update(position) > 0;
    }
}
